package kechengsheji.admin;

import MeetingRoom.MeetingRoomDAO;
import gongju.fuzhiqu;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AdminTablePanel extends JInternalFrame {

    private JTextField field1=new JTextField(10);
    private JComboBox<String> field2=new JComboBox<>();

    private JTable table = new JTable(){
        public boolean  isCellEditable(int row, int column){
            return false;
        }
    };
    public AdminTablePanel() {
        super("会议室列表", true, true, true, true);
        this.setVisible(true);

        JPanel topPanel=new JPanel();
        this.add(topPanel,BorderLayout.NORTH);

        topPanel.add(new JLabel("室名"));
        topPanel.add(field1);
        topPanel.add(new JLabel("是否使用"));
        topPanel.add(field2);

        field2.addItem("");
        field2.addItem(fuzhiqu.Close);
        field2.addItem(fuzhiqu.Open);

        JButton searchBtn = new JButton("查询");
        topPanel.add(searchBtn);

        JButton addBtn = new JButton("添加");
        topPanel.add(addBtn);

        JButton editBtn = new JButton("修改");
        topPanel.add(editBtn);

        JButton delBtn = new JButton("删除");
        topPanel.add(delBtn);

        JPanel panel = new JPanel(new BorderLayout());
        this.add(panel, BorderLayout.CENTER);
        panel.add(table, BorderLayout.CENTER);
        panel.add(table.getTableHeader(), BorderLayout.NORTH);
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        search();

        //鼠标点击事件（点击查询按钮，调用查询函数）
        searchBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                search();
            }
        });
        //鼠标点击事件（点击添加按钮，调用查询函数）
        addBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                Adminpanel.setContent(new AdminEditPanel(null));
            }
        });

        editBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int rowNum=table.getSelectedRow();
                if(rowNum <= -1){
                    return;
                }
                Adminpanel.setContent(new AdminEditPanel(table.getValueAt(rowNum,0)));
            }
        });

        delBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int rowNum=table.getSelectedRow();
                if(rowNum <= -1){
                    return;
                }
                MeetingRoomDAO.remove((int)table.getValueAt(rowNum,0));
                search();
            }
        });
    }

    //搜索会议室，并显示出来(可以按会议室名与是否可以使用来显示)
    private void search(){
        TableModel tablemodel = new DefaultTableModel(
                MeetingRoomDAO.searvhMulti(new int[]{1,3},new String[]{field1.getText(), (String) field2.getSelectedItem()}),MeetingRoomDAO.columnNames);
        table.setModel(tablemodel);
    }
}
